package fr.esipe.eioc3.timetableontime_v2;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.Log;

public class NetworkManager {
	
	Context context;
	
	/**
	 * CONSTRUCTEUR
	 * @param callerContext
	 */
	public NetworkManager(Context callerContext){
		context = callerContext;
	}
	
	public boolean isWifiEnabled(){
		
		ConnectivityManager connMg = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		
		NetworkInfo netInfo = connMg.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		
		return netInfo.isConnected();
	}
	
	public boolean isMobileDataEnabled(){
		
		ConnectivityManager connMg = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		
		NetworkInfo netInfo = connMg.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		
		return netInfo.isConnected();
	}
	
	public boolean checkIfNetworkIsAvailable(int idOfString){
		
		//Si le aucune connexion on rejete la requete
		if(!isWifiEnabled() && !isMobileDataEnabled()){
			
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
	
			builder.setMessage(idOfString).setIcon(R.drawable.ic_alerts_and_states_warning);
	
			builder.setNegativeButton(R.string.cancel_dialog, new DialogInterface.OnClickListener() {
	
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (!("google_sdk".equals(Build.PRODUCT))){
						((Activity)context).finish();
					}
				}
			});
			builder.show();
						
			Log.d("NetworkManager","network error ");
			return false;
		}
		else{
			Log.d("NetworkManager","network ok ");
			return true;

		}
	}
	
	

}
